'use strict';

const Controller = require('egg').Controller;


class LoginController extends Controller {
  async login() {
    const { ctx } = this;
    const userInfo = ctx.request.body;
    const result = await ctx.service.user.login(userInfo);
    ctx.body = result;
  }
  async getCode() {
    const { ctx } = this;
    const tel = ctx.params.tel;
    await ctx.service.userTelLogin.monitorTel(tel);
  }
  async telLogin() {
    const {
      ctx,
    } = this;
    const telInfo = ctx.request.body;
    await ctx.service.userTelLogin.telLogin(telInfo);
  }
  async index() {
    const { ctx } = this;
    const userToken = ctx.request.body;
    const {
      id,
    } = ctx.checkToken(userToken.token.split(' ')[1]);
    const userInfo = await ctx.service.user.getUser(id);
    if (userInfo) {
      ctx.body = {
        status: 200,
        data: {
          userid: userInfo.id,
          nickname: userInfo.nickname,
          userImg: userInfo.userimg,
          tel: userInfo.tel,
          email: userInfo.email,
          username: userInfo.username,
          sex: userInfo.sex,
          birthday: userInfo.birthday,
          province: userInfo.provinceaddress,
          city: userInfo.cityaddress,
          area: userInfo.areaaddress,
          hobby: userInfo.hobby,
          flag: true,
        },
      };
    } else {
      ctx.body = {
        status: 400,
        msg: '登录状态已过期',
      };
    }
  }
  async outLogin() {
    const { ctx, app } = this;
    const id = ctx.params.id;
    const result = await app.redis.del(id);
    await app.redis.del(id + 'u');
    if (result === 1) {
      ctx.body = {
        status: 200,
        msg: '用户退出成功',
        flag: false,
      };
    }
  }
}

module.exports = LoginController;
